package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.auth.LicenseModel;
import cn.com.yx.ms.domain.auth.LicensesUseModel;
import cn.com.yx.ms.domain.dbmodel.AuthClientLicense;
import cn.com.yx.ms.domain.dbmodel.SysRole;
import cn.com.yx.ms.service.ILicenseService;
import cn.com.yx.ms.service.IServerInfoService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.util.ZipUtil;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "licenses")
@Api(value = "License管理", description = "用于License管理的操作接口")
public class LicenseController {

    private static final Logger logger = LoggerFactory.getLogger(LicenseController.class);

    @Resource
    private IServerInfoService iServerInfoService;
    @Resource
    private ILicenseService iLicenseService;

    @RequestMapping("page")
    @ApiOperation("获取所有License数据")
    public ApiResult findLicenses(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        // 查询条件接受
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("licenseid", request.getParameter("licenseid"));
        String isuse=request.getParameter("isuse");
        if(null!=request.getParameter("isuse")){
            if(isuse.equals("-1")){
                isuse=null;
            }
        }
        query.getConditions().put("isuse",isuse);
        String endcondition=request.getParameter("endcondition");
        if (null!=endcondition){
            if(endcondition.equals("-1")){
                endcondition=null;
            }
        }
        query.getConditions().put("endcondition",endcondition);

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<LicenseModel> page = (Page<LicenseModel>) iServerInfoService.findLicenseModel(query);
        ApiPager<LicenseModel> apiPager = new ApiPager<LicenseModel>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @PostMapping("zip")
    @ApiOperation("导入License压缩包")
    public ApiResult importLicenseZip(MultipartHttpServletRequest request) throws Exception {
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                MsgPropertyUtil.getProperty(iLicenseService.importLicenseZip(request).msgId));
    }

    @PostMapping("uselicenses")
    @ApiOperation("设置本次使用的License数量")
    public ApiResult UseLicenses(@RequestBody List<LicensesUseModel> licensesUseModels) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                MsgPropertyUtil.getProperty(iLicenseService.UseLicense(licensesUseModels).msgId));
    }

    @PatchMapping("{licid}/{devid}/devchangeinfo")
    @ApiOperation("更换设备编号")
    public ApiResult DevInfoChange(@PathVariable("licid") String licid, @PathVariable("devid") String devid) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                MsgPropertyUtil.getProperty(iLicenseService.changeLicenseDevInfo(licid, devid).msgId));
    }

    @PatchMapping("{licid}/cleardevchangeinfo")
    @ApiOperation("更换设备编号")
    public ApiResult ClearDevInfoChange(@PathVariable("licid") String licid) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                MsgPropertyUtil.getProperty(iLicenseService.clearLicenseDevInfo(licid).msgId));
    }

    @PostMapping("exportzip")
    @ApiOperation("导出需要续费的License包")
    public ApiResult exportLicenseZip(@RequestBody List<AuthClientLicense> licids, HttpServletResponse res) throws Exception {

        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                iLicenseService.exportLicenseZipInfo(licids, res));

    }

    @RequestMapping("{cacheid}/licensezip")
    @ApiOperation("导出需要续费的License包")
    public void findLicenseZip(@PathVariable("cacheid") String cacheid, HttpServletResponse res) throws Exception {
        iLicenseService.downloadExportZipInfo(cacheid, res);
    }


}